<template>
  <div class="site-content">
    <el-dialog
      :title="'重置密码'"
      :close-on-click-modal="false"
      :visible.sync="visible">
      <el-form :model="dataForm" :rules="emailR" ref="emailItem" label-width="80px" @keyup.enter.native="dataFormSubmit()">
        <el-form-item label="邮箱" prop="Email" >
          <el-input v-model="dataForm.Email" placeholder="输入邮箱" label-width="50%"></el-input>
          <el-button style="margin-top: 10px" @click="sendEmail()" :disabled="send">
            <span v-show="!send">获取验证码</span>
            <span v-show="send" class="count">{{count}} s</span>
          </el-button>
        </el-form-item>
      </el-form>
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px" @keyup.enter.native="dataFormSubmit()">
        <el-form-item label="验证码" prop="checkcode">
          <el-input v-model="dataForm.checkcode" placeholder="验证码"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="Password">
          <el-input type="password" v-model="dataForm.Password" placeholder="请输入6-20位的密码，必须包含数字和字母，支持特殊符号~!@#$%^*"></el-input>
        </el-form-item>
        <el-form-item label="重复密码" prop="RePassword">
          <el-input type="password" v-model="dataForm.RePassword" placeholder="请重复输入密码"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
import { isEmail } from '@/utils/validate'
export default {
  data () {
    var validatePassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('密码不能为空'))
      } else if (this.passwordf !== false) {
        callback(new Error('密码格式不正确，请输入6-20位的密码，必须包含数字和字母，支持特殊符号~!@#$%^*'))
      } else {
        callback()
      }
    }
    var validateEmail = (rule, value, callback) => {
      if (!isEmail(value)) {
        callback(new Error('邮箱格式错误'))
      } else {
        callback()
      }
    }
    var validateComfirmPassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('确认密码不能为空'))
      } else if (this.dataForm.Password !== value) {
        callback(new Error('确认密码与密码输入不一致'))
      } else if (this.passwordf !== false) {
        callback(new Error('密码格式不正确，请输入6-20位的密码，必须包含数字和字母，支持特殊符号~!@#$%^*'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      send: false,
      timer: null,
      count: 60,
      passwordf: false,
      dataForm: {
        id: 0,
        Email: '',
        checkcode: '',
        Password: '',
        RePassword: ''
      },
      emailR: {
        Email: [
          { required: true, message: '邮箱名称不能为空', trigger: 'blur' },
          {validator: validateEmail, trigger: 'blur'}
        ]
      },
      dataRule: {
        // Email: [
        //   { required: true, message: '邮箱名称不能为空', trigger: 'blur' },
        //   {validator: validateEmail, trigger: 'blur'}
        // ],
        checkcode: [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
        ],
        RePassword: [
          {validator: validateComfirmPassword, trigger: 'blur'}
        ],
        Password: [
          {validator: validatePassword, trigger: 'blur'}
        ],
      }

    }
  },
  methods: {
    init (id) {
      this.visible = true
    },
    sendEmail () {
      this.$refs['emailItem'].validate((valid) => {
        if (valid) {
          if (this.timer === null) {
            this.send = !this.send
            this.timer = setInterval(() => {
              if (this.count > 0 && this.count <= 60) {
                this.count--;
              } else {
                this.send = !this.send;
                clearInterval(this.timer);  // 清除定时器
                this.timer = null;
              }
            }, 1000)
          }
          this.$http({
            url: this.$http.adornUrl('/put/sys_user/password'),
            method: 'post',
            params: {'email': this.dataForm.Email}
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message('成功！')
            }
          })
        }
      })
    },
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/put/sys_user/password/code'),
            method: 'post',
            data: this.$http.adornData({
              'email': this.dataForm.Email,
              'captcha': this.dataForm.checkcode,
              'password': this.dataForm.password
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                }
              })
            } else if (data && data.code === 10001) {
              this.passwordf = true
              this.$refs['dataForm'].validate()
              this.passwordf = false
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="scss">
  .site-content {
    min-height: 100%;
    padding: 30px 500px 30px 30px;
  }
</style>
